package ch.zbw.pra.sysventory.ui.gui.panels.settingspanel.subpanels;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import ch.zbw.pra.sysventory.ui.gui.panels.settingspanel.SettingsPanel;
import ch.zbw.pra.sysventory.ui.gui.utilities.GuiLayout;

public class SelectButtonPanel extends JPanel {

	private static final long serialVersionUID = -7565872627829206231L;
	private JButton selectAllButton;
	private JButton deselectAllButton;

	public SelectButtonPanel(SettingsPanel settingsPanel) {
		init();
		this.selectAllButton.addActionListener(new ButtonActionListener(settingsPanel, true));
		this.deselectAllButton.addActionListener(new ButtonActionListener(settingsPanel, false));
	}

	private void init() {
		this.selectAllButton = new JButton("Alle selektieren");
		this.deselectAllButton = new JButton("Alle deaktivieren");
		GridBagLayout gridBagLayout = GuiLayout.getGridBagLayout(this);
		GridBagConstraints gridBagConstraints = GuiLayout.getGridBagConstraints();
		double label = 1.0;
		double button = 0.2;
		int x = 0;
		int y = 0;
		gridBagLayout.columnWeights = new double[] { label, button, button, label };
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, new JLabel());
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, this.selectAllButton);
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, this.deselectAllButton);
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, new JLabel());

	}

	private class ButtonActionListener implements ActionListener {
		private boolean value;
		private SettingsPanel settingsPanel;

		public ButtonActionListener(SettingsPanel settingsPanel, boolean value) {
			this.settingsPanel = settingsPanel;
			this.value = value;
		}

		public void actionPerformed(ActionEvent e) {
			if (e.getSource() instanceof JButton) {
				this.settingsPanel.setValueToAllCheckBoxes(this.value);
			}
		}
	}
}
